package model

import (
	"errors"
	"myserver/app/types"
)

func CreateUserTable() {
	p := &types.User{}
	tables = append(tables, *p)
}

func AddUser(m *types.User) error {
	var err error
	if err = db.Create(m).Error; err != nil {
		return err
	}
	return err
}
func FindUserByUuid(strs []string) ([]types.User, error) {
	var users []types.User
	var err error
	tx := db.Begin()
	err = tx.Where("uuid IN ? AND is_del=?", strs, 0).Find(&users).Error
	if err != nil {
		tx.Rollback()
	} else {
		tx.Commit()
	}

	return users, err
}

func FindUserByAccount(strs []string, phones []string) ([]types.User, error) {
	var users []types.User
	var err error
	tx := db.Begin()
	slen := len(strs)
	plen := len(phones)
	if slen != 0 && plen != 0 {
		err = tx.Where("account IN ? AND phone IN ? AND is_del=?", strs, phones, 0).Find(&users).Error
	} else if slen != 0 {
		err = tx.Where("account IN ? AND is_del=?", strs, 0).Find(&users).Error
	} else if plen != 0 {
		err = tx.Where("phone IN ? AND is_del=?", strs, 0).Find(&users).Error
	} else {
		return users, errors.New("请传入用户名或者电话")
	}

	if err != nil {
		tx.Rollback()
	} else {
		tx.Commit()
	}

	return users, err
}

func UpDataUser(list *[]types.User) error {
	var err error
	tx := db.Begin()
	err = tx.Save(list).Error
	if err != nil {
		tx.Rollback()
	} else {
		tx.Commit()
	}
	return err
}
